package index

import (
	"fmt"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

type UserInfo struct {
	Name   string
	Gender string
	Age    int
}

type Links struct {
	Name string
	Link string
}

// 预定义模板函数
// 日期处理
func FormatAsDate(t time.Time) string {
	year, month, day := t.Date()
	return fmt.Sprintf("%d/%02d/%02d", year, month, day)
}

// 时间处理
func FormatAsTime(t time.Time) string {
	return fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
}

// 模板渲染示例
func TemplateHTMLDemo(c *gin.Context) {
	// 方式1
	var links []Links = []Links{
		{"唯一极客博客", "https://weiyigeek.top"},
		{"唯一极客主页", "https://blog.weiyigeek.top"},
	}
	// 方式2
	// links = append(links, Links{
	// 	Name: "唯一极客博客",
	// 	Link: "https://weiyigeek.top",
	// }, Links{
	// 	Name: "唯一极客主页",
	// 	Link: "https://blog.weiyigeek.top",
	// })

	c.HTML(http.StatusOK, "public/index.html", map[string]interface{}{
		"page_title":            "使用Gin演示HTML模板渲染的示例页面",
		"username":              "WeiyiGeek",
		"username_age":          18,
		"username_introduction": "从业安全运维开发等相关工作的专业人员，同时也是一名白帽黑客，专注于企业SecDevOps及网安研究、渗透测试等实践内容分享，负责保障企业网络系统、应用程序、数据安全、持续集成与交付系统等方面的稳定运行，励志于维护计算机和互联网安全。",
		"arg1":                  3,
		"arg2":                  5,
		"hobby":                 []string{"带娃", "吃饭", "睡觉", "coding"},
		"user": &UserInfo{
			Name:   "唯一极客",
			Gender: "Man",
			Age:    18,
		},
		"nowDateTime": time.Now(),
		"links":       links,
		"page_beian":  "渝ICP备2022003447号",
	})
}
